import json

import requests


def get_free_course_list_and_yxkc_list(cookie):
    free_course_list_post_header = {"Cookie": cookie}
    free_course_list_post_data = {'searchtj': '', 'xq': '0', 'jc': '0', 'kclbdm': ''}
    free_course_list_url = "http://jwcxk2.aufe.edu.cn/student/courseSelect/freeCourse/courseList"
    try:
        free_course_list_json = requests.post(free_course_list_url,
                                              headers=free_course_list_post_header,
                                              data=free_course_list_post_data).json()
    except:
        return [], []
    rwRxkZlList = json.loads(free_course_list_json["rwRxkZlList"])
    free_course_list = []
    for i in rwRxkZlList:
        kcm = "课程名:" + str(i["kcm"]) + "#"
        kch = "课程号:" + str(i["kch"]) + "#"
        kxh = "课序号:" + str(i["kxh"]) + "#"
        xf = "学分:" + str(i["xf"]) + "#"
        skjs = "上课教师:" + str(i["skjs"]) + "#"
        kslxmc = "考试类型:" + str(i["kslxmc"]) + "#"
        kkxsjc = "开课院系:" + str(i["kkxsjc"]) + "#"
        shangkedeshumue = "一周上课的次数:" + str(len(i["sjdd"])) + "#"
        skxq = "0_星期几上课:" + str(i["sjdd"][0]["skxq"]) + "#"
        skjc = "0_第几节课:" + str(i["sjdd"][0]["skjc"]) + "#"
        skzc = "0_上课的周:" + str(i["sjdd"][0]["skzc"]) + "#"
        cxjc = "0_上课持续的时间节:" + str(i["sjdd"][0]["cxjc"]) + "#"
        if len(i["sjdd"]) > 1:
            for o in range(1, len(i["sjdd"])):
                skxq += str(o) + "_星期几上课:" + str(i["sjdd"][o]["skxq"]) + "#"
                skjc += str(o) + "_第几节课:" + str(i["sjdd"][o]["skjc"]) + "#"
                skzc += str(o) + "_上课的周:" + str(i["sjdd"][o]["skzc"]) + "#"
                cxjc += str(o) + "_上课持续的时间节:" + str(i["sjdd"][o]["cxjc"]) + "#"
        bkskrl = "课程容量:" + str(i["bkskrl"]) + "#"
        bkskyl = "容量剩余:" + str(i["bkskyl"]) + "#"
        zxjxjhh = "教学计划年份:" + str(i["zxjxjhh"]) + "#"
        temp_str = kcm + kch + kxh + xf + skjs + bkskrl + bkskyl + kslxmc + kkxsjc + zxjxjhh \
                   + shangkedeshumue + skxq + skjc + skzc + cxjc
        free_course_list.append(temp_str)

    yxkclist = json.loads(free_course_list_json["yxkclist"])
    yxkc_list = []
    for i in yxkclist:
        attendClassTeacher = "教师:" + str(i["attendClassTeacher"]) + "#"
        courseName = "课程名称:" + str(i["courseName"]) + "#"
        examTypeName = "考试类型:" + str(i["examTypeName"]) + "#"
        coureNumber = "课程号:" + str(i["id"]["coureNumber"]) + "#"
        coureSequenceNumber = "课序号:" + str(i["id"]["coureSequenceNumber"]) + "#"
        unit = "学分:" + str(i["unit"]) + "#"
        coursePropertiesName = "课程属性:" + str(i["coursePropertiesName"]) + "#"
        selectCourseStatusName = "选课状态:" + str(i["selectCourseStatusName"]) + "#"
        executiveEducationPlanNumber = "0_教学计划年份:" + str(
            i["timeAndPlaceList"][0]["executiveEducationPlanNumber"]) + "#"
        shangkedeshumue = "一周上课的次数:" + str(len(i["timeAndPlaceList"])) + "#"
        classDay = "0_星期几上课:" + str(i["timeAndPlaceList"][0]["classDay"]) + "#"
        classSessions = "0_第几节课:" + str(i["timeAndPlaceList"][0]["classSessions"]) + "#"
        classWeek = "0_上课的周:" + str(i["timeAndPlaceList"][0]["classWeek"]) + "#"
        continuingSession = "0_上课持续的时间节:" + str(i["timeAndPlaceList"][0]["continuingSession"]) + "#"
        if len(i["timeAndPlaceList"]) > 1:
            for o in range(1, len(i["timeAndPlaceList"])):
                classDay += str(o) + "_星期几上课:" + str(i["timeAndPlaceList"][o]["classDay"]) + "#"
                classSessions += str(o) + "_第几节课:" + str(i["timeAndPlaceList"][o]["classSessions"]) + "#"
                classWeek += str(o) + "_上课的周:" + str(i["timeAndPlaceList"][o]["classWeek"]) + "#"
                continuingSession += str(o) + "_上课持续的时间节:" + str(
                    i["timeAndPlaceList"][o]["continuingSession"]) + "#"
        temp_str = courseName + attendClassTeacher + examTypeName \
                   + coureNumber + coureSequenceNumber + unit \
                   + coursePropertiesName + selectCourseStatusName + executiveEducationPlanNumber \
                   + shangkedeshumue + classDay + classSessions + continuingSession + classWeek
        yxkc_list.append(temp_str)
    return free_course_list, yxkc_list


def get_free_course_list_from_cookie(cookie):
    free_course_list, _ = get_free_course_list_and_yxkc_list(cookie)
    return free_course_list


def get_yxkc_list_from_cookie(cookie):
    _, yxkc_list = get_free_course_list_and_yxkc_list(cookie)
    return yxkc_list
